package com.lucweb.dao.impl;


import com.lucweb.dao.TellDao;

import com.lucweb.entity.TaskTell;
import org.hibernate.SessionFactory;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Repository("tellDao")
public class TellDaoImpl implements TellDao {


    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;


    @Override
    public List<TaskTell> tellList(String para) {
        List<TaskTell> tellList = (List<TaskTell>) sessionFactory.getCurrentSession().createQuery("FROM TaskTell where companyName ='"+para+"'").list();

        return tellList;
    }

    @Override
    public void saveOrUpdate(TaskTell taskCall) {
        sessionFactory.getCurrentSession().saveOrUpdate(taskCall);

    }

    @Override
    public TaskTell gettellById(int taskId) {
        return (TaskTell) sessionFactory.getCurrentSession().get(TaskTell.class, taskId);
    }

    @Override
    public void deletetell(int taskId) {

        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskTell.class, taskId)
        );
    }

    @Override
    public List<TaskTell> showTell() {
        List<TaskTell> tellList = (List<TaskTell>) sessionFactory.getCurrentSession().createQuery("FROM TaskTell").list();

        return tellList;
    }

    @Override
    public int getmaxId() {
        Integer number = (Integer) sessionFactory.getCurrentSession().createSQLQuery("select auto_increment from " +
                "information_schema.tables where table_schema='firesystem' and table_name='task_tell';")
                .addScalar("auto_increment", StandardBasicTypes.INTEGER).uniqueResult();
        return number;
    }
}
